
SWEP.Spawnable = true
SWEP.AdminSpawnable = false

SWEP.Author		= "Night-Eagle"
SWEP.Contact		= "gmail sedhdi"
SWEP.Purpose		= ""
SWEP.Instructions	= ""

SWEP.ViewModel = Model("models/weapons/v_toolgun.mdl")
SWEP.WorldModel = Model("models/weapons/w_toolgun.mdl")
SWEP.AnimPrefix = "python"
SWEP.ShootSound = Sound("Airboat.FireGunRevDown")
SWEP.MaxTrace = 65536

SWEP.Primary = {
	ClipSize = -1,
	DefaultClip = -1,
	Automatic = false,
	Ammo = "none",
}

SWEP.Secondary = {
	ClipSize = -1,
	DefaultClip = -1,
	Automatic = false,
	Ammo = "none",
}

function SWEP:Holster()
	return true
end

function SWEP:Deploy()
	return true
end

function SWEP:Initialize()
end

function SWEP:Think()
	if self.ThinkSub then
		self:ThinkSub()
	end
end

function SWEP:OnDrop()
	
end

function SWEP:OnRestore()
	//TODO: Make Luachip work with saving and loading
end

function SWEP:Precache()
	Model(self.ViewModel)
	Model(self.WorldModel)
	Sound(self.ShootSound)
end

function SWEP:Reload()
	if not self.Owner:KeyPressed(IN_RELOAD) then
		return
	end
end

function SWEP:DoShootEffect(hitpos,hitnormal,entity,physbone)
	//Allow direct passage of trace result table
	if type(hitpos) == "table" then
		hitnormal = hitpos.HitNormal
		entity = hitpos.Entity
		physbone = hitpos.PhysicsBone
		
		hitpos = hitpos.HitPos
	end
	
	//Below ripped from toolgun
	self.Weapon:EmitSound(self.ShootSound)
	self.Weapon:SendWeaponAnim(ACT_VM_PRIMARYATTACK)
	
	local fx = EffectData()
		fx:SetOrigin(hitpos)
		fx:SetNormal(hitnormal)
		fx:SetEntity(entity)
		fx:SetAttachment(physbone)
	util.Effect("selection_indicator",fx)
	
	local fx = EffectData()
		fx:SetOrigin(hitpos)
		fx:SetStart(self.Owner:GetShootPos())
		fx:SetAttachment(1)
		fx:SetEntity(self.Weapon)
	util.Effect("ToolTracer",fx)
	
end

function SWEP:Trace(aim,start,endpos,filter,mask)
	return util.TraceLine{
		start = start or self.Owner:GetShootPos(),
		endpos = endpos or (self.Owner:GetShootPos() + (aim or self.Owner:GetAimVector())*self.MaxTrace),
		filter = filter or self.Owner,
		mask = mask or (CONTENTS_SOLID|CONTENTS_MOVEABLE|CONTENTS_MONSTER|CONTENTS_WINDOW|CONTENTS_DEBRIS|CONTENTS_GRATE|CONTENTS_AUX),
	}
end

function SWEP:ContextScreenClick(aim,mc,pressed,pl)
	if not pressed then
		return
	end
	
	// No prediction in single player
	if CLIENT and SinglePlayer() then
		return
	end 
	
	local tr = self:Trace()
	self:DoShootEffect(tr)
end

function SWEP:PrimaryAttack()
	local tr = self:Trace()
	self:DoShootEffect(tr)
	
	if CLIENT then
		return
	end
	
	if luachip.playerAccess(self.Owner,tr.Entity) and IsEntity(tr.Entity) then
		if not tr.Entity:IsWorld() then
			self:SetNWEntity("curEnt",tr.Entity)
			if luachip and luachip.plent then
				luachip.plent[self.Owner] = tr.Entity
				luachip.xmitLinkedEnts(self.Owner,tr.Entity)
			end
		/*
		else
			self:SetNWEntity("curEnt",nil)
			if luachip and luachip.plent then
				luachip.plent[self.Owner] = nil
			end
		*/
		end
	end
end

function SWEP:SecondaryAttack()
	local tr = self:Trace()
	self:DoShootEffect(tr)
	
	if CLIENT then
		return
	end
	
	if IsEntity(tr.Entity) then
		umsg.Start("lcswepalt",self.Owner)
			umsg.Entity(tr.Entity)
		umsg.End()
	end
end

























































